<?php
# Framework
require 'fw'.DIRECTORY_SEPARATOR.'init.php';

# Application
define('APP', dirname(__file__).DIRECTORY_SEPARATOR);
define('TITLE', 'Image Wheel');
define('VERSION', '1.3');

# Configuration
fw::config(array
(
	'upload_limit'	=>	15,
	'file_limit'	=>	2097152,
	'anonymous'		=>	1,
	'dimensions'	=>	'2048x2048'
));

# Helpers
fw::helper('Template', 'Form');

# Template
Template::layout('layout.html');
Template::script('jquery', 'auth');

# Language
fw::lang('main');

# Formatting
function size($bytes)
{
	$units = array('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
	
	for($i=1; $i<=9; $i++)
	{
		if($bytes*$i <= pow(1024, $i)) return round($bytes/pow(1024, $i-1), 1).' '.$units[$i-1].'B';
	}
}

# User
if(isset($_GET['log_out']))
{
	User::unauthenticate();
}

if(!User::is_authenticated())
{
	$log_in = User::log_in();
	$register = User::register();
	$forgot = User::forgot();
	
	if($register->submitted && $register->valid)
	{
		Template::script_code('$("#register_button").click();');
	}
	
	if($forgot->submitted && $forgot->valid)
	{
		Template::script_code('$("#forgot_button").click();');
	}
}

if(User::is_authenticated())
{
	$profile = User::profile();
}

# Routing
$routing = array
(
	# Uploader
	''					=>	array('Upload', 'uploader'),
	# Gallery
	'gallery\/?'												=>	array('Gallery', 'browse'),
	'gallery\/(new|popular)\/(all|month|week|today)\/(\d+)\/?'	=>	array('Gallery', 'browse'),
	# View
	'([a-zA-Z]+)\/?'	=>	array('Gallery', 'view'),
	# Configure
	'([a-z0-9]{13})\/?'					=>	array('Upload', 'configure'),
	'([a-z0-9]{13})\/([a-zA-Z]+)\/?'	=>	array('Upload', 'configure'),
	# User images
	'user\/(['.lang('alphanumeric').'a-zA-Z0-9_]{3,12})\/?'													=>	array('Gallery', 'user'),
	'user\/(['.lang('alphanumeric').'a-zA-Z0-9_]{3,12})\/(new|popular)\/(all|month|week|today)\/(\d+)\/?'	=>	array('Gallery', 'user')
);

if(fw::config('anonymous') == '0' && !User::is_authenticated())
{
	$routing[''] = $routing['gallery\/?'];
}

fw::routing($routing);